package cn.saltybe.analysis.controller.api.statistic;

import cn.saltybe.analysis.entity.result.PageView;
import cn.saltybe.analysis.service.PageViewServiceImpl;
import cn.saltybe.analysis.util.ApiResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/api/statistic/page-view")
public class PageViewAPI {
    private PageViewServiceImpl pageViewService;

    @Autowired
    public void setPageViewService(PageViewServiceImpl pageViewService) {
        this.pageViewService = pageViewService;
    }

    @ResponseBody
    @GetMapping(value = "list")
    public String list() throws Exception {
        List<PageView> list;
        list = pageViewService.all();
        JsonArray result = new Gson().toJsonTree(list).getAsJsonArray();
        return ApiResponse.buildSuccessResponse(result).toString();
    }
}
